package com.dianrui.agent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.DeviceList;
import com.dianrui.agent.util.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DeviceListAdapter extends BaseAdapter {
    DeviceClickListener deviceClickListener;
    Context context;
    List<DeviceList> deviceList;

    public DeviceListAdapter(Context context, List<DeviceList> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    public void refresh(List<DeviceList> deviceLists) {
        this.deviceList = deviceLists;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (deviceList == null) {
            return 0;
        }
        return deviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface DeviceClickListener {

        void showLocation(String usestatus,String glat, String glng, String number, String vehicle_status, int battery);

        void ClickInfo(String vehicleId);
        void showCode(String qrUrl);

    }

    public void setOndeviceClickListener(DeviceClickListener deviceClickListener) {
        this.deviceClickListener = deviceClickListener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (deviceList.size() > 0) {
            final DeviceList vehicleListBean = deviceList.get(position);
            GlideUtil.loadImageView(context.getApplicationContext(), vehicleListBean.qr_code, holder.qrCode);
            holder.deviceNumber.setText(vehicleListBean.number);
            String showOnlinestatus = "在线情况: " + vehicleListBean.online_status;
            String showUseStatus = "使用状态: " + vehicleListBean.use_status;
            String showBattery = "剩余电量: " + vehicleListBean.battery + "%";
            String showCondition = "当前车况: " + vehicleListBean.condition;
            holder.onlineStatus.setText(showOnlinestatus);
            holder.useStatus.setText(showUseStatus);
            holder.battery.setText(showBattery);
            holder.condition.setText(showCondition);
            holder.showLocation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deviceClickListener.showLocation(vehicleListBean.use_status,vehicleListBean.glat, vehicleListBean.glng, vehicleListBean.number, vehicleListBean.vehicle_status, vehicleListBean.battery);
                }
            });
            holder.mainDeviceLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    deviceClickListener.ClickInfo(vehicleListBean.vehicle_id);
                }
            });
            holder.qrCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deviceClickListener.showCode(vehicleListBean.qr_code);
                }
            });

        }

        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.qr_code)
        ImageView qrCode;
        @BindView(R.id.device_number)
        TextView deviceNumber;
        @BindView(R.id.online_status)
        TextView onlineStatus;
        @BindView(R.id.use_status)
        TextView useStatus;
        @BindView(R.id.battery)
        TextView battery;
        @BindView(R.id.show_location)
        LinearLayout showLocation;
        @BindView(R.id.condition)
        TextView condition;
        @BindView(R.id.main_device_layout)
        LinearLayout mainDeviceLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
